<?php

namespace app\admin\services;
use think\facade\Request;
use think\Service;
use think\facade\Session;
use app\admin\model\BaseServiceModel;
use think\facade\Cache;
class BaseService extends Service
{
    public function __construct(Request $request = null)
    {
        $this->BaseServiceModel = new BaseServiceModel;
        parent::__construct($request);
    }


    /**
     * @name 验证session是否存在以及过期
     */
    public function checkSession()
    {
        if (empty(Session::get('uid','admin'))) {
            return false;
        }
        return true;
    }


    /**
     * @name 验证session 是否和数据中相等
     */
    public function checkSessionValue()
    {
        $sessionid = Session::get('sessionid','admin');
        $data = $this->BaseServiceModel->findAdmin(Session::get('uid','admin'));
        if ($sessionid != $data['session_id']) {
            return false;
        }
        return true;
    }

    /**
     * @name 验证cookie 是否存在以及过期
     */

    public function checkey()
    {
        $key = Session::get('uid','admin');
        $data = Cache::store('redis')->get('login_id' . $key);
        if (!$data) {
            return false;
        }
        $member = $this->BaseServiceModel->findAdmin(Session::get('uid','admin'));
        if ($data != $member['session_id']) {
            return false;
        }
        Cache::store('redis')->set('login_id' . $key, $data, 4 * 3600);
        return true;
    }
}